class CycleController < ApplicationController

  before_filter :login_required
  
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @cycle_pages, @cycles = paginate :cycles, :per_page => 10
  end

  def show
    @cycle = Cycle.find(params[:id])
  end
  
  def show_seeker_form
    @cycle = Cycle.find(params[:id])
  end
  
  def show_admin_view
    @cycle = Cycle.find(params[:id])
  end
  
  def show_manager_view
    @cycle = Cycle.find(params[:id])
  end                
  
  def show_judge_view
    @cycle = Cycle.find(params[:id])
  end

  def new
    @form = Form.find(params[:id])
                                        
    if not @form
      flash[:notice] = "The form you chose must have an error."   
      redirect_back_or_default(:controller => 'form', :action => 'list')
    end
    
    @cycle = Cycle.new   
    @cycle.form = @form  
    @cycle.name = @form.name
    @cycle.seeker_form = @form.seeker_form
    @cycle.manager_view = @form.manager_view
    @cycle.judge_view = @form.judge_view
    @cycle.admin_view = @form.admin_view
    @cycle.description = @form.description
  end

  def create
    @cycle = Cycle.new(params[:cycle])
    if @cycle.save
      flash[:notice] = 'Cycle was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @cycle = Cycle.find(params[:id])
  end

  def update
    @cycle = Cycle.find(params[:id])
    
    if @cycle.update_attributes(params[:cycle])
      flash[:notice] = 'Cycle was successfully updated.'
      redirect_to :action => 'show', :id => @cycle
    else
      render :action => 'edit'
    end
  end

  def destroy                       
    if current_user.is_admin?
      Cycle.find(params[:id]).destroy
    end
    redirect_to :action => 'list'
  end
end
